EquipList={};
--[[local DEquipList={
	{["Name"]="Helmet",},{["Name"]="Hands",},{["Name"]="Feet",},
	{["Name"]="Upper Body",},{["Name"]="Lower Body",},{["Name"]="Cape",},
	{["Name"]="Belt",},{["Name"]="Shoulder",},
	{["Name"]="left Earrings",},
	{["Name"]="Ammunition",},
	{["Name"]="Ranged Weapon",},
	{["Name"]="right Earrings",},
	{["Name"]="Necklace",},
	{["Name"]="left Ring",},{["Name"]="right Ring",},
	{["Name"]="Main Hand",},{["Name"]="Off Hand",},
	{["Name"]="Sammelwerkzeuge",},
	{["Name"]="Talisman 1",},{["Name"]="Talisman 2",},{["Name"]="Talisman 3",},
};]]
 
function XBarEQDmg_OnLoad(this)
	this:RegisterEvent("LOADING_END");
	SaveVariables("EquipList");
end

function XBarEQDmg_OnEvent(this,event)
-- ChatFrame1:AddMessage("XBar-EQDmg : "..event);
	if (event=="LOADING_END") then
		this:RegisterEvent("PLAYER_INVENTORY_CHANGED");
	end
	if (event=="PLAYER_INVENTORY_CHANGED") then
		local worstID=0;
		local worstDur=100;
		local avgDur=0;
		local itemCount=0;
		for index=0,21 do
			local X={};
			if (GetInventoryItemTexture("player",index)) then
				getglobal("XBarEQDmg_Slot"..index.."_Icon"):SetTexture(GetInventoryItemTexture("player",index));
			else getglobal("XBarEQDmg_Slot"..index.."_Icon"):SetTexture("Interface\\Icons\\Geq.tga"); end
			getglobal("XBarEQDmg_Slot"..index):SetID(index);
			X["curD"],X["maxD"],X["itemName"],X["curDfull"],X["maxDfull"]=GetInventoryItemDurable("player",index);
			EquipList[index]=X;
			if X["maxD"]~=0 then
				itemCount=itemCount+1;
				local TEMP_DUR=math.ceil((X["curD"]/X["maxD"])*1000)/10;
				if TEMP_DUR<worstDur then
					worstDur=TEMP_DUR;
					worstID=index;
				end
				getglobal("XBarEQDmg_Slot"..index.."_Text"):SetText(XBarEQDmg_SetColor(TEMP_DUR)..TEMP_DUR.."%|r");
				avgDur=avgDur+TEMP_DUR;
			end
		end
		if itemCount==0 then return; end
		avgDur=math.ceil((avgDur/itemCount)*10)/10;
		if (XLng["Config"]) then XBarEQDmg_AvgName:SetText(XLng["Ttip"]["AvgTmp"]);
		else XBarEQDmg_AvgName:SetText("avg. durability :"); end
		XBarEQDmg_AvgDura:SetText(XBarEQDmg_SetColor(avgDur)..avgDur.."%|r");
	end
end

function XBarEQDmg_OnShow(this)
	if (XBar["EQDmg_X"]==-1 and XBar["EQDmg_Y"]==-1) then
		this:SetScale(1);
		this:ClearAllAnchors();
		if (XBar["Position"]=="TOP") then this:SetAnchor("TOPLEFT","BOTTOMLEFT","XBarKlass",0,0);
		elseif (XBar["Position"]=="BOTTOM") then this:SetAnchor("BOTTOMLEFT","TOPLEFT","XBarKlass",0,0); end
	else
		local scale=GetUIScale();
		this:SetScale(XBar["EQDmg_Scale"]);
		this:ClearAllAnchors();
		this:SetAnchor("TOPLEFT","TOPLEFT","UIParent",(XBar["EQDmg_X"]/scale)/XBar["EQDmg_Scale"],(XBar["EQDmg_Y"]/scale)/XBar["EQDmg_Scale"]);
	end
end

function XBarEQDmg_OnHover(this)
	GameTooltip:SetOwner(XBarEQDmg,"ANCHOR_TOPRIGHT",0,0);
	GameTooltip:SetInventoryItem("player",this:GetID());
end

function XBarEQDmg_OnMove(this,set)
	if (set) then this:StartMoving();
	else
		this:StopMovingOrSizing();
		XBar["EQDmg_X"],XBar["EQDmg_Y"]=this:GetPos();
	end
end

function XBarEQDmg_Scale(this,set)
	local scale=this:GetParent():GetScale();
	if (set=="UP" and scale<5.0) then this:GetParent():SetScale(scale+.1); end
	if (set=="DN" and scale>.19) then this:GetParent():SetScale(scale-.1); end
	XBar["EQDmg_Scale"]=this:GetParent():GetScale();
end

function XBarEQDmg_SetColor(durability)
	if (durability<=20) then return "|cffFF0000";
	elseif (durability<=40) then return "|cffFF0F4B";
	elseif (durability<=60) then return "|cffFFD24D";
	elseif (durability<=80) then return "|cffCCFF33";
	elseif (durability<=99) then return "|cff55FF55";
	else return "|cffFFFFFF"; end
end
